﻿using System;
using System.IO;
using Ersur.DevTeam;

namespace Ersur
{
    public partial class Settings : XPopupForm
    {
        public Settings()
        {
            InitializeComponent();
        }

        static FileInfo fi = new FileInfo("c:\\Ersur\\Ersur.txt");

        public static string ConnectionString;
        public static string ConfigFilePath;

        public static string TerminalNo;
        public static string FairNo;
        public static string FairName;
        public static string Season;
        public static string Notes;
        public static string Currency;
        public static string MailSubject;
        public static string MailBody;
        public static string ComPort;
        public static decimal Parity;       
        public static string Server;

        public static void Init()
        {
            string[] lines = File.ReadAllLines(fi.FullName);
            ConfigFilePath = fi.FullName;

            foreach (string line in lines)
            {
                string clearedLine = line.Trim().Replace("\t", "");

                if (clearedLine.StartsWithCI("TerminalNo"))
                    TerminalNo = clearedLine.ExtractValue("TerminalNo");

                else if (clearedLine.StartsWithCI("FairName"))
                    FairName = clearedLine.ExtractValue("FairName");

                else if (clearedLine.StartsWithCI("FairNo"))
                    FairNo = clearedLine.ExtractValue("FairNo");

                else if (clearedLine.StartsWithCI("Season"))
                    Season = clearedLine.ExtractValue("Season");

                else if (clearedLine.StartsWithCI("Notes"))
                    Notes = clearedLine.ExtractValue("Notes");

                else if (clearedLine.StartsWithCI("Currency"))
                    Currency = clearedLine.ExtractValue("Currency");

                else if (clearedLine.StartsWithCI("MailSubject"))
                    MailSubject = clearedLine.ExtractValue("MailSubject");

                else if (clearedLine.StartsWithCI("MailBody"))
                    MailBody = clearedLine.ExtractValue("MailBody");

                else if (clearedLine.StartsWithCI("ComPort"))
                    ComPort = clearedLine.ExtractValue("ComPort");

                else if (clearedLine.StartsWithCI("Parity"))
                    Parity = decimal.Parse(clearedLine.ExtractValue("Parity"));

                else if (clearedLine.StartsWithCI("Server"))
                    Server = clearedLine.ExtractValue("Server");
            }

            ConnectionString = "Data Source=.\\sqlExpress;Initial Catalog=Ersur;User ID=ErsurUser;Password=10341518;Persist Security Info=True";

            //TODO: server bağlantısı değişmeli
            if (Server == "Sargun")
                ConnectionString = "Data Source=.\\SRGNExpress;Initial Catalog=Ersur;User ID=ErsurUser;Password=10341518;Persist Security Info=True";


            //if (Server != null && Server.ToLower() == "server")
            //    ConnectionString = "Data Source=.\\sqlExpress;Initial Catalog=Ersur;User ID=ErsurUser;Password=10341518;Persist Security Info=True";
        }

        private void Settings_Load(object sender, EventArgs e)
        {
            txtTerminalNo.Text = TerminalNo;
            txtFairNo.Text = FairNo;
            txtFair.Text = FairName;
            txtSeason.Text = Season;
            txtNotes.Text = Notes;
            txtCurrency.Text = Currency;
            txtMailSubject.Text = MailSubject;
            txtMailBody.Text = MailBody;
            txtCom.Text = ComPort;
            txtParity.Text = Parity.ToString();            
            cmbServer.SelectedItem = Server;
        }

     

        private void btnSave_Click(object sender, EventArgs e)
        {
            TerminalNo = txtTerminalNo.Text;
            FairNo = txtFairNo.Text;
            FairName = txtFair.Text;
            Season = txtSeason.Text;
            Notes = txtNotes.Text;
            Currency = txtCurrency.Text;
            MailSubject = txtMailSubject.Text;
            MailBody = txtMailBody.Text;
            ComPort = txtCom.Text;
            Parity = decimal.Parse(txtParity.Text);
           
            Server = cmbServer.SelectedItem.ToString();

            string settings = "";
            settings += "TerminalNo\t\t\t" + txtTerminalNo.Text + Environment.NewLine;
            settings += "FairName\t\t\t" + txtFair.Text + Environment.NewLine;
            settings += "FairNo\t\t\t" + txtFairNo.Text + Environment.NewLine;
            settings += "Season\t\t\t" + txtSeason.Text + Environment.NewLine;
            settings += "Notes\t\t\t" + txtNotes.Text + Environment.NewLine;
            settings += "Currency\t\t\t" + txtCurrency.Text + Environment.NewLine;
            settings += "MailSubject\t\t\t" + txtMailSubject.Text + Environment.NewLine;
            settings += "MailBody\t\t\t" + txtMailBody.Text + Environment.NewLine;
            settings += "ComPort\t\t\t" + txtCom.Text + Environment.NewLine;
            settings += "Parity\t\t\t" + txtParity.Text + Environment.NewLine;            
            settings += "Server\t\t\t" + cmbServer.SelectedItem.ToString() + Environment.NewLine;

            File.WriteAllText(fi.FullName, settings);
            this.Hide();
        }
    }
}